package com.example.yiwei.crm.view.activity.JYCoordingActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.MyLocationData;
import com.example.yiwei.crm.R;
import com.example.yiwei.crm.base.baseClass;
import com.example.yiwei.crm.dao.dockDao;
import com.example.yiwei.crm.dao_impl.dockDaoImpl;
import com.example.yiwei.crm.data.GetVisit;
import com.example.yiwei.crm.entity.dockingEntity;
import com.example.yiwei.crm.entity.managerEntity;
import com.example.yiwei.crm.util.HttpAssist;
import com.example.yiwei.crm.util.NetWorkUtils;
import com.example.yiwei.crm.util.SC;
import com.example.yiwei.crm.view.Class.gridViewClass.GetLong;
import com.example.yiwei.crm.view.Class.listviewClass.CoorSelectAdapter;
import com.example.yiwei.crm.view.Class.listviewClass.ListAdapter1;
import com.example.yiwei.crm.view.activity.jdjycooractivity.JYJDAddManyiduActivity;
import com.example.yiwei.crm.view.activity.jtCustomer.JDcoorActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class JYAddVisitActivity extends AppCompatActivity {
    private EditText edit_theme,edit_visit_gift,edit_present_value,edit_additional;
    private ImageView img_back;
    private TextView txt_title;
    private  String result;
    String coorname;
    int coor_id;
    private ArrayAdapter adapter2;
    List<com.example.yiwei.crm.entity.dockingEntity> coorlist= new ArrayList<>();
    private List<String> coor_name_list = new ArrayList<>();
    private  String result1;
    private String priority,task_state,manager_name;
    private int priority_id,task_state_id,manager_id;
    com.example.yiwei.crm.entity.dockingEntity dockingEntity;
    com.example.yiwei.crm.entity.scheduleEntity scheduleEntity;
    com.example.yiwei.crm.entity.personnalEntity personnalEntity;
    private SharedPreferences sp;
    List<managerEntity> managerList;
    List<String> managerlist;
    int i1 =1;
    int i2 =1;
    int i3 = 1;
    String fanwei;
    String msg;
    double range;
    private ImageView img_search,img_luru,img_me,img_tj;
    private Button button;
    private RelativeLayout coor_rl;
    private ImageView img_paizhao,img_show;
    private File mPhotoFile;
    private String mPhotoPath;
    public final static int CAMERA_RESULT = 1;
    private String KPA15,KPA14;
    private ImageView img_xinghao;
    int juli_int;
    private ImageView img_coor_select;
    private PopupWindow mPopWindow;
    private EditText edit_search;
    private Button btn_search;
    private ListView listView;
    private CoorSelectAdapter adapter;
    private TextView show_coor,txt_dingwei;
    private LinearLayout linearLayout_dingwei;
    StringBuffer sb;
    private String lo1,la1,lo2,la2;
    private double jd,wd,jd1,wd1;
    private String address;
    List<com.example.yiwei.crm.entity.dockingEntity> dockingList;
    String str;
    String result3;
    double s;
    double juli;
    private TextView txt_line;
    public LocationClient mLocationClient = null;
    public BDLocationListener myListener = new MyLocationListener();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jyadd_visit);
        linearLayout_dingwei= (LinearLayout) findViewById(R.id.line1_dingwei);
        txt_line= (TextView) findViewById(R.id.txt_dingwei_line);
        show_coor = (TextView) findViewById(R.id.show_coor_name);
        img_coor_select = (ImageView) findViewById(R.id.img_xiangxia);
        txt_dingwei= (TextView) findViewById(R.id.health_education_add_txt_dingwei);
        sp = getSharedPreferences("login", Context.MODE_PRIVATE);
        img_xinghao= (ImageView) findViewById(R.id.xinghao);
        img_paizhao = (ImageView) findViewById(R.id.add_img_btn);
        img_show = (ImageView) findViewById(R.id.show_img);
        img_paizhao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");//开始拍照
                    mPhotoPath = getSDPath()+"/"+ getPhotoFileName();//设置图片文件路径，getSDPath()和getPhotoFileName()具体实现在下面
                    mPhotoFile = new File(mPhotoPath);
                    if (!mPhotoFile.exists()) {
                        mPhotoFile.createNewFile();//创建新文件
                    }
                    intent.putExtra(MediaStore.EXTRA_OUTPUT,//Intent有了图片的信息
                            Uri.fromFile(mPhotoFile));
                    startActivityForResult(intent, CAMERA_RESULT);//跳转界面传回拍照所得数据
                } catch (Exception e) {
                    Log.d("ccc",e.toString());
                }
            }
        });
        if ("update".equals(getIntent().getStringExtra("tag"))){
            img_xinghao.setVisibility(View.GONE);
            linearLayout_dingwei.setVisibility(View.GONE);
            txt_line.setVisibility(View.GONE);
        }else{
            img_xinghao.setVisibility(View.VISIBLE);
            linearLayout_dingwei.setVisibility(View.VISIBLE);
            txt_line.setVisibility(View.VISIBLE);
        }
        img_search= (ImageView) findViewById(R.id.item_image_cx);
        coor_rl = (RelativeLayout) findViewById(R.id.add_visit_coor_rl);
        button = (Button) findViewById(R.id.add_visit_btn);
        img_luru= (ImageView) findViewById(R.id.item_image_lr);
        img_back= (ImageView) findViewById(R.id.add_visit_img_back);
        img_tj= (ImageView) findViewById(R.id.item_image_tj);
        img_me= (ImageView) findViewById(R.id.item_image_w);
        txt_title= (TextView) findViewById(R.id.healt_education_add_txt_title);
        edit_additional= (EditText) findViewById(R.id.add_visit_edit_addtional);
        edit_present_value= (EditText) findViewById(R.id.add_visit_edit_present_value);
        edit_visit_gift= (EditText) findViewById(R.id.add_visit_edit_visit_gift);
        edit_theme= (EditText) findViewById(R.id.add_visit_edit_theme);
        mLocationClient = new LocationClient(getApplicationContext());
        //声明LocationClient类
        mLocationClient.registerLocationListener( myListener );
        initLocation(); //设置定位参数
        mLocationClient.start(); // 开始定位
        edit_present_value.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        if ("update".equals(getIntent().getStringExtra("tag"))){
            scheduleEntity = GetVisit.scheduleEntity;
            coor_rl.setVisibility(View.GONE);
        }
        if ("update".equals(getIntent().getStringExtra("tag"))){
            txt_title.setText("修改拜访信息");
        }
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        img_me.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(JYAddVisitActivity.this,JDcoorActivity.class);
                intent.putExtra("me","me");
                startActivity(intent);
                finish();
            }
        });
        img_tj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(JYAddVisitActivity.this,JDcoorActivity.class);
                intent.putExtra("tt","tt");
                startActivity(intent);
                finish();
            }
        });
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
////                result= NetWorkUtils.getCount(baseClass.find_visit_spinner_url,null);
////                dockDao dockDao=new dockDaoImpl();
////                YXJBSpinnerList=dockDao.queryDockingSpinnerMessage(result,1);
////                ZTSpinnerList=dockDao.queryDockingSpinnerMessage(result,2);
//                SC sc = new SC();
//                JSONObject object = sc.storage(sp);
//                String json1 = object.toString();
//                dockDao dockDao = new dockDaoImpl();
//                coorlist = dockDao.queryAllCoordinationMessage(baseClass.find_coordination_url + 1, json1);
//                try {
//                    object.put("admin",sp.getString("admin",null));
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
//                String json = object.toString();
//                runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
//                        if (coorlist != null) {
//                            for (int i3 = 0; i3 < coorlist.size(); i3++) {
//                                if (coorlist.get(i3).getDocking_mechanism()!=null&&!coorlist.get(i3).getDocking_mechanism().equals("")){
//                                    coor_name_list.add(coorlist.get(i3).getDocking_mechanism()+"-"+coorlist.get(i3).getDocking_name());
//                                }else{
//                                    coor_name_list.add(coorlist.get(i3).getDocking_name());
//                                }
//                            }
//                            adapter2 = new ArrayAdapter(JYAddVisitActivity.this, R.layout.spinner_display_style, R.id.txtvwSpinner, coor_name_list);
//                            adapter2.setDropDownViewResource(R.layout.spinner_dropdown_style);
//                            spinner_coor.setAdapter(adapter2);
//                            spinner_coor.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
//                                @Override
//                                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//                                    coorname = coorlist.get(position).getDocking_name();
//                                    coor_id = coorlist.get(position).getDocking_id();
//                                }
//
//                                @Override
//                                public void onNothingSelected(AdapterView<?> parent) {
//
//                                }
//                            });
//                            if ("update".equals(getIntent().getStringExtra("tag"))) {
//                                for (int i = 0; i < coorlist.size(); i++) {
//                                    if (coorlist.get(i).getDocking_id() == scheduleEntity.getDocking_id()) {
//                                        spinner_coor.setSelection(i);
//                                    }
//                                }
//                            }
//                        }
//                    }
//
//
//                });
//            }
//        }).start();
        img_coor_select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        SC sc = new SC();
                        JSONObject object = sc.storage(sp);
                        String json = object.toString();
                        dockDao dockDao = new dockDaoImpl();
                        coorlist = dockDao.queryAllCoordinationMessage(baseClass.find_coordination_url + 1, json);
                        if (coorlist!=null) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    if (coorlist != null) {
                                        adapter = new CoorSelectAdapter(JYAddVisitActivity.this, coorlist);
                                        showPopupWindow();
                                    }
                                }
                            });
                        }else{
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(JYAddVisitActivity.this, "没有合作客户，请新建合作客户！", Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                    }
                }).start();
            }
        });
        if ("update".equals(getIntent().getStringExtra("tag"))){
            edit_theme.setText(scheduleEntity.getSchedule_theme());
            edit_visit_gift.setText(scheduleEntity.getVisit_gift());
            edit_present_value.setText(scheduleEntity.getGift_value());
            edit_additional.setText(scheduleEntity.getDirector_required());
        }
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                button.setVisibility(View.GONE);
                if (!"update".equals(getIntent().getStringExtra("tag"))&&coorname==null){
                    Toast.makeText(JYAddVisitActivity.this, "请选择合作伙伴", Toast.LENGTH_SHORT).show();
                    button.setVisibility(View.VISIBLE);
                }else if (!"".equals(edit_visit_gift.getText().toString())){
                    if ("".equals(edit_present_value.getText().toString())||"".equals(edit_additional.getText().toString())){
                        button.setVisibility(View.VISIBLE);
                        Toast.makeText(JYAddVisitActivity.this, "请填写主任要求和礼品价值", Toast.LENGTH_SHORT).show();
                    }else{
                        if ("update".equals(getIntent().getStringExtra("tag"))){
                            uploadImage();
                        }else{
                            add2();
                        }

                    }
                }else{
                    if ("update".equals(getIntent().getStringExtra("tag"))){
                        uploadImage();
                    }else{
                        add2();
                    }
                }
            }
        });

        img_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(JYAddVisitActivity.this,JDcoorActivity.class);
                intent.putExtra("ii","ii");
                startActivity(intent);
                finish();
            }
        });
        img_luru.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(JYAddVisitActivity.this,JDcoorActivity.class);
                startActivity(intent);
                finish();
            }
        });

    }

    public void add2(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                result3=NetWorkUtils.getCount(baseClass.find_fanwei,null);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            JSONObject object=new JSONObject(result3);
                            String boolen=object.getString("ifsuccess");
                            if ("true".equals(boolen)){
                                 fanwei=object.getString("range");
                                if (fanwei.equals("")){

                                }else{
                                    range=Double.parseDouble(fanwei);
                                }
                                 juli=GetLong.getlong(jd,wd,jd1,wd1);
                                if (range!=0){
                                    if (juli>range){
                                        Toast.makeText(JYAddVisitActivity.this, "超过限定范围无法新增拜访！", Toast.LENGTH_SHORT).show();
                                        button.setVisibility(View.VISIBLE);
                                    }else{
                                        uploadImage();

                                    }
                                }

                            }else{
                                Toast.makeText(JYAddVisitActivity.this, "范围值获取失败", Toast.LENGTH_SHORT).show();
                                button.setVisibility(View.VISIBLE);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });

            }
        }).start();

    }
    public  void add(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                SC sc = new SC();
                JSONObject object=sc.storage(sp);
                try {
                    object.put("KPA02",edit_theme.getText().toString());
                    object.put("CID03",sp.getString("CID03",null));
                    object.put("KPA09",edit_visit_gift.getText().toString());
                    if (edit_present_value.getText().toString()!=null&&!edit_present_value.getText().toString().equals("")){
                        object.put("KPA10",edit_present_value.getText().toString());
                    }
                    if ("update".equals(getIntent().getStringExtra("tag"))){
                        //协作人拜访修改
                        object.put("addoredit","edit");
                        object.put("KPA01",scheduleEntity.getVisit_id());
                        object.put("KXA01",scheduleEntity.getDocking_id());
                    }else {
                        if(manager_id==0){
                            object.put("KPA03",sp.getString("UAB01",null));
                            object.put("KPA11",sp.getString("UAB03",null));
                        }else {
                            object.put("KPA03",sp.getString("CID01",null));
                            object.put("KPA11",sp.getString("CID03",null));
                        }
                        object.put("addoredit","add");
                        object.put("KXA01",coor_id);
                    }
                    object.put("KPA14",edit_additional.getText().toString());
                    object.put("KPA16",KPA15);
                    object.put("KPA15",KPA14);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String json=object.toString();
                if ("health".equals(getIntent().getStringExtra("TAG"))){
                    //对接人添加拜访接口
                    result1= NetWorkUtils.getCount(baseClass.add_visit_url,json);
                }
                result1 = NetWorkUtils.getCount(baseClass.edit_BF_URL,json);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            JSONObject object1=new JSONObject(result1);
                            boolean boolen=object1.getBoolean("ifsuccess");
                            if(boolen){
                                finish();
                                Toast.makeText(JYAddVisitActivity.this, "操作成功", Toast.LENGTH_SHORT).show();
                            }else {
                                msg=object1.getString("msg");
                                if ("update".equals(getIntent().getStringExtra("tag"))){
                                    Toast.makeText(JYAddVisitActivity.this, "操作失败", Toast.LENGTH_SHORT).show();
                                }else {
                                    if ("".equals(msg)) {
                                        button.setVisibility(View.VISIBLE);
                                        Toast.makeText(JYAddVisitActivity.this, "操作失败", Toast.LENGTH_SHORT).show();
                                    } else {
                                        button.setVisibility(View.VISIBLE);
                                        Toast.makeText(JYAddVisitActivity.this, msg, Toast.LENGTH_SHORT).show();
                                    }
                                }

                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
//                        finish();
                    }
                });

            }
        }).start();
    }
    public static String getTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);

    }
    public void uploadImage(){
        if (mPhotoPath==null){
            if ("update".equals(getIntent().getStringExtra("tag"))){
                add();
            }else {
                button.setVisibility(View.VISIBLE);
                Toast.makeText(JYAddVisitActivity.this, "照片不存在，请拍照后重试！！", Toast.LENGTH_SHORT).show();
            }
        }else {
            Toast.makeText(JYAddVisitActivity.this, "信息正在上传请稍候！！", Toast.LENGTH_SHORT).show();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    File file = new File(mPhotoPath);
                    if (file.exists()){
                        Log.d("ccc",file.getName());
                        String URL=baseClass.upload_img_URL+"type=kpa1&UAB02="+sp.getString("UAB02",null);
                        final String s = HttpAssist.uploadFile(file,URL);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    JSONObject object = new JSONObject(s);
                                    boolean b = object.getBoolean("ifsuccess");
                                    if (b){
                                        JSONObject object2 = object.getJSONObject("list");
                                        KPA15 = object2.getString("KPA16");
                                        KPA14 = object2.getString("KPA15");
                                        add();
                                        Toast.makeText(JYAddVisitActivity.this, KPA14, Toast.LENGTH_SHORT).show();
                                    }else {
                                        button.setVisibility(View.VISIBLE);
                                        Toast.makeText(JYAddVisitActivity.this, "照片上传失败！！请重试", Toast.LENGTH_SHORT).show();

                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                            }
                        });
                    }else {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if ("update".equals(getIntent().getStringExtra("tag"))){
                                    add();
                                }else {
                                    button.setVisibility(View.VISIBLE);
                                    Toast.makeText(JYAddVisitActivity.this, "照片不存在，请拍照后重试！！", Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    }
                }
            }).start();
        }
    }
    public String getSDPath(){
        File sdDir = null;
        boolean sdCardExist = Environment.getExternalStorageState()
                .equals(Environment.MEDIA_MOUNTED);   //判断sd卡是否存在
        if   (sdCardExist)
        {
            sdDir = Environment.getExternalStorageDirectory();//获取跟目录
        }

        return sdDir.toString();

    }


    private String getPhotoFileName() {

        return sp.getString("UAB02",null)+".jpg";
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.d("sssss",resultCode+"");
        if(resultCode == resultCode){//对应第二种方法
            /**
             * 这种方法是通过内存卡的路径进行读取图片，所以的到的图片是拍摄的原图
             */
            File fis = null;
            try {
                //把图片转化为字节流
                fis = new File(mPhotoPath);
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 4;
//                //把流转化图片
                Bitmap bitmap = BitmapFactory.decodeFile(mPhotoPath,options);

                img_show.setImageBitmap(bitmap);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    private void showPopupWindow() {
        View contentView = LayoutInflater.from(JYAddVisitActivity.this).inflate(
                R.layout.popuwindoe_select_coor, null);

        mPopWindow = new PopupWindow(contentView);
        mPopWindow.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        mPopWindow.setHeight( LinearLayout.LayoutParams.WRAP_CONTENT);

        contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int popupWidth = contentView.getMeasuredWidth();    //  获取测量后的宽度
        final int popupHeight = contentView.getMeasuredHeight();  //获取测量后的高度
        int[] location = new int[2];
        edit_search = (EditText) contentView.findViewById(R.id.edit_seacher);
        btn_search = (Button) contentView.findViewById(R.id.btn_show);
        listView = (ListView) contentView.findViewById(R.id.btn_listview);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                coorname = coorlist.get(position).getDocking_name();
                coor_id = coorlist.get(position).getDocking_id();
                show_coor.setText(coorname);
                if (coorlist.get(position).getJingdu()!=null){
                    jd1=Double.parseDouble(coorlist.get(position).getJingdu());
                    wd1=Double.parseDouble(coorlist.get(position).getWd());
                }
                mPopWindow.dismiss();
//                mPopWindow.setOnDismissListener(new poponDismissListener());
            }
        });
        btn_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        SC sc = new SC();
                        JSONObject object = sc.storage(sp);
                        try {
                            object.put("KXA02",edit_search.getText().toString());
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        String json = object.toString();
                        dockDao dockDao = new dockDaoImpl();
                        coorlist = dockDao.queryAllCoordinationMessage(baseClass.find_coordination_url + 1, json);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (coorlist==null){
                                    Toast.makeText(JYAddVisitActivity.this, "搜索不到信息！！", Toast.LENGTH_SHORT).show();
                                }else {
                                    adapter = new CoorSelectAdapter(JYAddVisitActivity.this,coorlist);
                                    listView.setAdapter(adapter);
                                }
                                adapter.notifyDataSetChanged();
                            }
                        });
                    }
                }).start();
            }
        });
        img_coor_select.getLocationOnScreen(location);
        mPopWindow.setAnimationStyle(R.style.contextMenuAnim);
        mPopWindow.setBackgroundDrawable(getResources().getDrawable(
                R.drawable.pop_bg));
        mPopWindow.setTouchable(true);
        mPopWindow.setOutsideTouchable(true);   //设置外部点击关闭ppw窗口
        mPopWindow.setFocusable(true);
        mPopWindow.update();
        mPopWindow.showAsDropDown(img_coor_select);
        backgroundAlpha(0.5f);
        mPopWindow.setOnDismissListener(new poponDismissListener());
    }
    private void initLocation(){
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy
        );//可选，默认高精度，设置定位模式，高精度，低功耗，仅设备
        option.setCoorType("bd09ll");//可选，默认gcj02，设置返回的定位结果坐标系
        int span=1000;
        option.setScanSpan(span);//可选，默认0，即仅定位一次，设置发起定位请求的间隔需要大于等于1000ms才是有效的
        option.setIsNeedAddress(true);//可选，设置是否需要地址信息，默认不需要
        option.setOpenGps(true);//可选，默认false,设置是否使用gps
        option.setLocationNotify(true);//可选，默认false，设置是否当GPS有效时按照1S/1次频率输出GPS结果
        option.setIsNeedLocationDescribe(true);//可选，默认false，设置是否需要位置语义化结果，可以在BDLocation.getLocationDescribe里得到，结果类似于“在北京天安门附近”
        option.setIsNeedLocationPoiList(true);//可选，默认false，设置是否需要POI结果，可以在BDLocation.getPoiList里得到
        option.setIgnoreKillProcess(false);//可选，默认true，定位SDK内部是一个SERVICE，并放到了独立进程，设置是否在stop的时候杀死这个进程，默认不杀死
        option.SetIgnoreCacheException(false);//可选，默认false，设置是否收集CRASH信息，默认收集
        option.setEnableSimulateGps(false);//可选，默认false，设置是否需要过滤GPS仿真结果，默认需要
        mLocationClient.setLocOption(option);
    }
    public class MyLocationListener implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {

            //获取定位结果
            StringBuffer sb = new StringBuffer(256);

            sb.append(location.getTime()+",");
            sb.append(location.getLocationDescribe()+",");// 位置语义化信息
            sb.append(location.getLatitude()+",");    //获取纬度信息
            sb.append(location.getLongitude());    //获取经度信息
            if (sb!=null) {
                str = sb.toString();
                String[] strs = str.split(",");
                jd = Double.parseDouble(strs[3]);
                wd = Double.parseDouble(strs[2]);
                address=strs[1];
                if ("在郑和雕像附近".equals(address)){
                    if ("update".equals(getIntent().getStringExtra("tag"))){

                    }else{
                        Toast.makeText(JYAddVisitActivity.this, "请检查网络，并重新定位", Toast.LENGTH_SHORT).show();
                        txt_dingwei.setText("");
                    }

                }else if (address.equals("null")){
                   txt_dingwei.setText("");
                } else{
                    txt_dingwei.setText(address);
                }
//              sb=null;
            }else{
                txt_dingwei.setText("");
            }
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mLocationClient.stop();
    }
    public void backgroundAlpha(float bgAlpha)
    {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha= bgAlpha;
        getWindow().setAttributes(lp);
    }
    class poponDismissListener implements PopupWindow.OnDismissListener{

        @Override
        public void onDismiss() {
            // TODO Auto-generated method stub
            //Log.v("List_noteTypeActivity:", "我是关闭事件");
            backgroundAlpha(1f);
        }

    }

}
